import Base from 'models/base';
import SystemUtil from 'utils/systemUtil'

import moment from 'moment';

class GoodsGroupData {
  constructor(data = []) {
    this.id = SystemUtil.getId();
    this.count = data.length;
    if (data.length > 0) {
      this.sponsorUnionid = data[0].sponsorUnionid;
    } else {
      this.sponsorUnionid = null;
    }

    this.personList = this.getPersonList(data);
  }

  getPersonList(data) {
    let personList = [];
    for (let i = 0; i < 4; i++) {
      let obj = data[i];
      let person = this.getPersonData(obj);
      personList.push(person);
    }
    return personList;
  }

  getPersonData(obj) {
    if (obj === undefined) {
      return {};
    }
    return {
      name: obj.nickname,
      url: obj.headimgurl
    };
  }

  resetPersonData(group = []) {
    let count = group.length;
    if (this.count === count) {
      return;
    }
    this.count = count;
    this.personList = this.getPersonList(group);
  }
}

export default class GoodsGroupDetail extends Base {
  constructor(goods, systemTime) {
    super();

    this.defId = goods.goodsDefId;
    this.modelId = goods.goodsModelId;
    this.name = goods.goodsName;
    this.price = goods.activityPrice;
    this.description = goods.goodsDescription;
    this.tip = goods.goodsTips;

    let sponsorUnionid = goods.sponsorUnionid;
    if (SystemUtil.isEmpty(sponsorUnionid)) {
      sponsorUnionid = '';
    }
    this.sponsorUnionid = sponsorUnionid;

    let pic = goods.goodsPic;
    this.pics = pic.split(',');

    let startTime = moment(goods.activityStartTime).valueOf();
    this.startTime = startTime;

    let endTime = moment(goods.activityEndTime).valueOf();
    this.endTime = endTime;

    this.recommendGroupList = [];
    this.myGroup = null;

    this.setGroupType(systemTime);
  }

  setGroupType(nowTime) {
    if (nowTime === undefined) {
      nowTime = moment();
    } else {
      nowTime = moment(nowTime)
    }

    let startTime = this.startTime;
    let endTime = this.endTime;
    let type;
    if (moment(nowTime).isBefore(startTime, 'second')) {
      type = 0;
    } else if (moment(nowTime).isAfter(endTime, 'second')) {
      type = 2;
    } else {
      type = 1;
    }
    this.type = type;
  }

  setRecommendGroupList(groupList) {
    let recommendGroupList = [];
    for (let i = 0; i < groupList.length; i++) {
      let group = groupList[i];
      group = new GoodsGroupData(group);
      recommendGroupList.push(group);
    }
    if (recommendGroupList.length === 0) {
      recommendGroupList.push(new GoodsGroupData());
    }
    this.recommendGroupList = recommendGroupList;
  }

  setMyGroup(group) {
    let myGroup = this.myGroup;
    if (this.sponsorUnionid === '') {
      this.sponsorUnionid = group[0].sponsorUnionid;
    }
    if (myGroup === null) {
      this.myGroup = new GoodsGroupData(group);
    } else {
      myGroup.resetPersonData(group);
    }
  }

  getPayObject() {
    let postData = {
      goodsDefId: this.defId,
      goodsModelId: this.modelId,
      buyCount: 1,
      activityTypeId: 2
    };

    return postData;
  }
}
